<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Configurations extends Admin_Controller{

	function Configurations(){
        parent::Admin_Controller();
        $CI = & get_instance();        
        $CI->load->module_model('configurations.configuration_model');
        $CI->load->module_language('configurations.configurations');
        $this->module_navigation('General Options');
	}
	function index()
	{
		$this->general_lists();
	}
	function general_lists(){	        
        $CI = & get_instance();
        $base_url = 'admin/configurations.configurations.general_lists';
		$option = "GENERAL";
		list($limit, $offset, $sort_field, $sort_type) = array($CI->config->item('rows_per_page'), 3, 4 ,5);        
        $this->_lists($base_url,$option, $CI->config->item('rows_per_page'), $offset, $sort_field, $sort_type);        
        $this->module_toolbar('configurations.toolbar');
        $this->module_navigation('General Options');
	}
	function search_engine_lists(){	        
        $CI = & get_instance();
        $base_url = 'admin/configurations.configurations.search_engine_lists';
		$option = "SEARCH_ENGINE";
		list($limit, $offset, $sort_field, $sort_type) = array($CI->config->item('rows_per_page'), 3, 4 ,5);        
        $this->_lists($base_url,$option, $CI->config->item('rows_per_page'), $offset, $sort_field, $sort_type);        
        $this->module_toolbar('configurations.toolbar');
        $this->module_navigation('Search Engine Options');
	}
	
	function _lists($base_url,$option, $limit, &$offset=0, &$sort_field=PARAMETER_PARAM_ID, &$sort_type='0'){
        $CI = & get_instance();
        $CI->load->library('form_validation');       

        $config['uri_segment'] = $offset;
        $offset = $CI->uri->segment($offset)?$CI->uri->segment($offset):0;
        $sort_field = $CI->uri->segment($sort_field)?$CI->uri->segment($sort_field):PARAMETER_PARAM_ID;
        $sort_type = $CI->uri->segment($sort_type)?'1':'0';		
        $data = $CI->configuration_model->get_lists($option,$limit, $offset, $sort_field, $sort_type);        
        
        
        foreach($data as &$item){			
			$val = $CI->input->post('ids')?$CI->input->post($item->param_id . '_value'):$item->value;
			$type = $item->type;
	        if($type == 'BUBBLES'){
	        	$type = 'RADIO/OFF;ON';        	
	        }
	        if($type == 'NEWCLIENTFEE' || $type == 'CLASSSIZE'){
				$CI->form_validation->set_rules($item->param_id . '_value', 'Value', 'trim|numeric');				
				$type = 'INPUT';	
			} else {
				$CI->form_validation->set_rules($item->param_id . '_value', 'Value', 'trim');
			}
	        if($type == 'INFO')
	        	$type = 'INPUT';
					
	        if($type == 'MAKEUPCLAS'){
				$type = 'RADIO/YES;NO';
			}
	        
	        $type_data = '';
	        $arr_type = split('/', $type);
	        if(is_array($arr_type)){
				$type = $arr_type[0];	
				$type_data = isset($arr_type[1])?$arr_type[1]:'';
			} else {
				$type = $arr_type;	
			}
	
	        switch($type){
				case 'INPUT' : 
					$dat = array(
		              'name'        => $item->param_id . '_value',
		              'id'          => $item->param_id . '_value',
		              'value'       => $val,
		              'maxlength'   => '100',
		              'size'        => '50',              
		            );
		            $item->value = form_input($dat);
		            break;	            
		        case 'RADIO' :
		        	$option = split(";", $type_data);
		        	$value = '';
		        	
		        	$cnt = 0;
		        	foreach($option as $v)
		        	{
		        		$checked = FALSE;
		        		
		        		if($val == $v || empty($val)){
							$checked = TRUE; 
						}
						$dat = array(
						    'name'        => $item->param_id . '_value',
						    'id'          => $item->param_id . '_value' . "_" . $cnt,
						    'value'       => $v,
						    'checked'     => $checked,					    
					    );
					    $value .= form_radio($dat) . form_label($v, $dat['id']);
					    
					    $cnt ++;
					}
					$item->value = $value;
					break;
				case 'TEXTAREA' : 
					$dat = array(
		              'name'        => $item->param_id . '_value',
		              'id'          => $item->param_id . '_value',
		              'value'       => $val,
		              'maxlength'   => '100',
		              'rows'        => '4',
					  'cols'        => '40',
		            );
		            $item->value = form_textarea($dat);
		            break;
		        case 'FCKEDITOR' :
		        	$CI->load->library('fckeditor');        
			        $CI->fckeditor->Value = $val;
			        $CI->fckeditor->Width = 345;
			        $CI->fckeditor->Height = 120;
			        $CI->fckeditor->ToolbarSet = 'Basic';
			        $CI->fckeditor->SetPathOfFranchise();
			        
			        $CI->fckeditor->InstanceName = $item->param_id . '_value';		        
			        $item->value =  $CI->fckeditor->CreateHtml();
			        break;
			    case 'CUST_DROPDOWN' :
			    	$CI->load->module_model('page2_css.page2_css_dropdown_model');
					$CI->page2_css_dropdown_model->name = $item->param_id . '_value';
					$CI->page2_css_dropdown_model->value = $val;
					$CI->page2_css_dropdown_model->addOption(array(PAGE_CSS_CSS_FILE=>'', PAGE_CSS_CSS_FILE=>'----- Select Colour -----'));
					//$item->value = Modules::run('page2_css.page2_css.dropdown');
					$item->value = Modules::run('configurations.configurations.dropdown');
			        break;
			} 	
			$dat = array(
				'value'       => $CI->input->post('ids')?$CI->input->post($item->param_id . '_description'):$item->description ,
		    	'maxlength'   => '80',
		    	'size'		  => '60',
		    	'id'		  => $item->param_id . '_description',
				'name'		  => $item->param_id . '_description',	
			);
			$item->description = form_input($dat);
            $item->type = $type;
		}		
		
        $this->add('data', $data);

        $CI->load->library('pagination');
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$sort_type);
        $config['total_rows'] = $CI->configuration_model->get_numrows();
        $config['per_page'] = $limit;
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
        
		
		$this->add('param_idTXT', $this->sort_column('#', PARAMETER_PARAM_ID, $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('nameTXT', $this->sort_column('Name', PARAMETER_NAME, $sort_type, $base_url.'/'.$offset, $sort_field));
       
       $flag = true;
       if($CI->input->post('ids')){
        	if ($CI->form_validation->run() !== FALSE){
        	
	    		$ids =  $CI->input->post('ids');
	    		$types =  $CI->input->post('types');
	    		$ids = split(",", $ids);
	    		$types = split(",", $types);
	    		
	    		foreach($ids as $k=>$id){
	    			if($id == 0) continue;	
					$data = array(
						PARAMETER_FRANCHISE_ID => $CI->configuration_model->admin_id,
						PARAMETER_PARAM_ID => $id,					
						PARAMETER_VALUE => $CI->input->post($id . '_value'),
						PARAMETER_DESCRIPTION => $CI->input->post($id . '_description')                          
		            );
		            $CI->configuration_model->set_id($id);
	            	if(!$CI->configuration_model->save_by_fid($data))      
						$flag = false;
                    if($types[$k] == 'CUST_DROPDOWN'){
                        $CI->load->module_helper('administrator.franchises.theme_xml');
                        $CI->load->module_model('administrator.franchises.franchise');
                        $CI->franchise->get_by_fid();
                        generate_theme_xml($CI->configuration_model->admin_id, $CI->franchise->franchise_title, $CI->franchise->franchise_type, $CI->input->post($id . '_value'));
                    }
				}
				   
				if($flag)
					set_flash_message(lang('configuration_edit_success'));
				else
					set_flash_message(lang('configuration_edit_fail'));    
				
			} else {
				set_flash_message(lang('configuration_edit_fail'));     
			}
	        redirect($CI->uri->uri_string());
        }
       $this->add('base_url', $base_url);
        $this->view('lists');
    }
    
    function dropdown()
	{
		$CI = & get_instance();
		
		// model
		$CI->load->module_model('page2_css.page2_css_dropdown_model');
		
		// add to view
		$this->add('name', $CI->page2_css_dropdown_model->name);
		$this->add('onchange', $CI->page2_css_dropdown_model->onchange);
		$this->add('value', $CI->page2_css_dropdown_model->value);
		$this->add('items', $CI->page2_css_dropdown_model->items);
		
		// view
		$this->view('page2_css_dropdown_view');
	}
    
    function edit()
    {
		$CI = & get_instance();        
        $CI->load->library('form_validation');
        $CI->load->helper('form');        
        
        $id = $CI->uri->segment(3);
        $CI->configuration_model->set_id($id);
        if(!$CI->configuration_model->get_by_fid()){
            set_flash_message(lang('configuration_edit_not_found'));
            redirect('admin/configurations/');
        }
        $this->add('name', $CI->configuration_model->name);
        $val = ($CI->input->post('mode'))?$CI->input->post('value'):$CI->configuration_model->value;
        $this->add('value', $val);
        
        
        $type = $CI->configuration_model->type;
        if($type == 'BUBBLES'){
        	$type = 'RADIO/OFF;ON';        	
        }
        if($type == 'NEWCLIENTFEE' || $type == 'CLASSSIZE'){
			$CI->form_validation->set_rules('value', 'Value', 'trim|numeric');
			$type = 'INPUT';	
		} else {
			$CI->form_validation->set_rules('value', 'Value', 'trim');
		}
        if($type == 'INFO')
        	$type = 'INPUT';
				
        if($type == 'MAKEUPCLAS'){
			$type = 'RADIO/YES;NO';
		}
        
        $type_data = '';
        $arr_type = split('/', $type);
        if(is_array($arr_type)){
			$type = $arr_type[0];	
			$type_data = isset($arr_type[1])?$arr_type[1]:'';
		} else {
			$type = $arr_type;	
		}
		
		
    	//var_dump($type); exit;    	
        switch($type){
			case 'INPUT' : 
				$data = array(
	              'name'        => 'value',
	              'id'          => 'value',
	              'value'       => $val,
	              'maxlength'   => '100',
	              'size'        => '50',              
	            );
	            $this->add('value', form_input($data));
	            break;	            
	        case 'RADIO' :
	        	$option = split(";", $type_data);
	        	$value = '';
	        	
	        	$cnt = 0;
	        	foreach($option as $v)
	        	{
	        		$checked = FALSE;
	        		
	        		if($val == $v || empty($val)){
						$checked = TRUE; 
					}
					$data = array(
					    'name'        => 'value',
					    'id'          => 'value_' . $cnt,
					    'value'       => $v,
					    'checked'     => $checked,					    
				    );
				    $value .= form_radio($data) . form_label($v, $data['id']);
				    
				    $cnt ++;
				}
				$this->add('value', $value);
				break;
			case 'TEXTAREA' : 
				$data = array(
	              'name'        => 'value',
	              'id'          => 'value',
	              'value'       => $val,
	              'maxlength'   => '100',
	              'rows'        => '8',
				  'cols'        => '40',
	            );
	            $this->add('value', form_textarea($data));
	            break;
	        case 'FCKEDITOR' :
	        	$CI->load->library('fckeditor');        
		        $CI->fckeditor->Value = $val;
		        $CI->fckeditor->Width = 800;
		        $CI->fckeditor->Height = 300;
		        $CI->fckeditor->SetPathOfFranchise();
		        
		        $CI->fckeditor->InstanceName = 'value';		        
		        $this->add('value', $CI->fckeditor->CreateHtml());
		        break;
		    case 'CUST_DROPDOWN' :
		    	$CI->load->module_model('page2_css.page2_css_dropdown_model');
				$CI->page2_css_dropdown_model->name = 'value';
				$CI->page2_css_dropdown_model->value = $val;
				$CI->page2_css_dropdown_model->addOption(array(PAGE_CSS_CSS_FILE=>'', PAGE_CSS_CSS_FILE=>'----- Select Colour -----'));
				//$this->add('value', Modules::run('page2_css.page2_css.dropdown'));
				$this->add('value', Modules::run('configurations.configurations.dropdown'));
		        break;
		}        
       
        if($CI->input->post('mode')){
        	if ($CI->form_validation->run() !== FALSE){
        	
	    		$data = array(
						  PARAMETER_VALUE => $CI->input->post('value'),
	                    );
		        if($CI->configuration_model->save_by_fid($data)){                
		            set_flash_message(lang('configuration_edit_success'));                
		        } else {
					set_flash_message(lang('configuration_edit_fail'));     
				}
				redirect('admin/configurations/');
			}
	        
        }
        
        
        
        $this->module_navigation(get_class($this).' : '.lang('configuration_edit_title'));        
        $this->view('edit');
        
	}
    
    
    function contact_list(){	        
        $CI = & get_instance();
        $base_url = 'admin/configurations.configurations.contact_list';
		list($limit, $offset, $sort_field, $sort_type) = array($CI->config->item('rows_per_page'), 3, 4 ,5);        
        $this->_contact_list($base_url, $CI->config->item('rows_per_page'), $offset, $sort_field, $sort_type);        
        $this->module_toolbar('configurations.toolbar_contactform');
        $this->module_navigation('Contact form parameters');
	}
	
    function _contact_list($base_url, $limit, &$offset=0, &$sort_field=PARAMETER_PARAM_ID, &$sort_type='0'){
		$CI = & get_instance();
        
        $config['uri_segment'] = $offset;
        $offset = $CI->uri->segment($offset)?$CI->uri->segment($offset):0;
        $sort_field = $CI->uri->segment($sort_field)?$CI->uri->segment($sort_field):PARAMETER_PARAM_ID;
        $sort_type = $CI->uri->segment($sort_type)?'1':'0';		
        $data = $CI->configuration_model->get_contact_lists($limit, $offset, $sort_field, $sort_type);
        $this->add('data', $data);

        $CI->load->library('pagination');
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$sort_type);
        $config['total_rows'] = $CI->configuration_model->get_numrows();
        $config['per_page'] = $limit;
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
        $this->add('type', 1);
		
		$this->add('param_idTXT', $this->sort_column('#', PARAMETER_PARAM_ID, $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('nameTXT', $this->sort_column('Name', PARAMETER_NAME, $sort_type, $base_url.'/'.$offset, $sort_field));
       
        $this->view('list_lists');
	}
	
	function delete(){
        $CI = & get_instance();
        $CI->configuration_model->set_id($CI->uri->segment(3));
        $type = $CI->uri->segment(4);
        
        $CI->configuration_model->delete_by_fid();        
        set_flash_message(lang('configuration_delete_success'));
        if($type == 1)
        	redirect('admin/configurations.configurations.contact_list'); 
		else	
        	redirect('admin/configurations.configurations.mkt_src_list');       
        
    }
    function delete_all(){	
		$CI = & get_instance();
		$type = $CI->input->post('type');	
        if($CI->input->post('act') == 'delete_all'){
            $cnt = 0;
            foreach($CI->input->post('uid') as $id){
                $CI->configuration_model->set_id($id);
                $CI->configuration_model->delete_by_fid();
                $cnt++;
            }
            set_flash_message(sprintf(lang('configuration_del_all_success'), $cnt));            
            if($type == 1)
        		redirect('admin/configurations.configurations.contact_list');
        	else	
        		redirect('admin/configurations.configurations.mkt_src_list');
        }
	}
	function add_new(){
        $CI = & get_instance();
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules(PARAMETER_VALUE, 'Value', 'trim|required');
        
        $type = $CI->uri->segment(3);
        if(empty($type)){
			$type = $CI->input->post('type');
		}
        $parameter_name = $type == 1 ? TYPE_CONTACT_FORM : TYPE_MKT_SRC;
        if ($CI->form_validation->run() !== FALSE){
            $data = array(
            				PARAMETER_FRANCHISE_ID => $CI->configuration_model->admin_id,
            				PARAMETER_NAME => $parameter_name,
            				PARAMETER_TYPE => 'LIST',
							PARAMETER_VALUE => $CI->input->post(PARAMETER_VALUE),
							PARAMETER_DESCRIPTION => $CI->input->post(PARAMETER_DESCRIPTION)                          
                          );
            if($CI->configuration_model->save($data)){                
                set_flash_message(lang('configuration_add_success'));                
            } else {
				set_flash_message(lang('configuration_add_fail'));     
			}
			if($type == 1)
        		redirect('admin/configurations.configurations.contact_list');
        	else	
        		redirect('admin/configurations.configurations.mkt_src_list');
            
        }
        
        $this->add(PARAMETER_VALUE, ($CI->input->post(PARAMETER_VALUE)));
        $this->add(PARAMETER_DESCRIPTION, ($CI->input->post(PARAMETER_DESCRIPTION)));       
        $this->add('type', $type);
        
        $this->module_navigation(get_class($this).' : '.lang('configuration_add_title'));
        $this->view('add_new');
    }
    
    function edit_contact(){
        $CI = & get_instance();
        $CI->load->library('form_validation');
        $CI->load->helper('form');
        $CI->form_validation->set_rules(PARAMETER_VALUE, 'Value', 'trim|required');
        
        $type = $CI->uri->segment(4);
        if(empty($type)){
			$type = $CI->input->post('type');
		}
        
        $id = $CI->uri->segment(3);
        $CI->configuration_model->set_id($id);
        if(!$CI->configuration_model->get_by_fid()){
            set_flash_message(lang('configuration_edit_not_found'));
            if($type == 1)
        		redirect('admin/configurations.configurations.contact_list');
        }
        $this->add(PARAMETER_VALUE, ($CI->input->post('mode'))?$CI->input->post(PARAMETER_VALUE):$CI->configuration_model->value);
        $this->add(PARAMETER_DESCRIPTION, ($CI->input->post('mode'))?$CI->input->post(PARAMETER_DESCRIPTION):$CI->configuration_model->description);
        $this->add('type', $type);
        $parameter_name = $type == 1 ? TYPE_CONTACT_FORM : TYPE_MKT_SRC;
        if ($CI->form_validation->run() !== FALSE){
            $data = array(
            				PARAMETER_FRANCHISE_ID => $CI->configuration_model->admin_id,
            				PARAMETER_NAME => $parameter_name,
            				PARAMETER_TYPE => 'LIST',
							PARAMETER_VALUE => $CI->input->post(PARAMETER_VALUE),
							PARAMETER_DESCRIPTION => $CI->input->post(PARAMETER_DESCRIPTION)                          
                          );
            if($CI->configuration_model->save($data)){                
                set_flash_message(lang('configuration_add_success'));                
            } else {
				set_flash_message(lang('configuration_add_fail'));     
			}
			if($type == 1)
        		redirect('admin/configurations.configurations.contact_list');
        	else	
        		redirect('admin/configurations.configurations.mkt_src_list');	
            
        }                
        $this->module_navigation(get_class($this).' : '.lang('configuration_add_title'));
        $this->view('add_new');
    }
    
    function mkt_src_list(){	        
        $CI = & get_instance();
        $base_url = 'admin/configurations.configurations.mkt_src_list';
		list($limit, $offset, $sort_field, $sort_type) = array($CI->config->item('rows_per_page'), 3, 4 ,5);        
        $this->_mkt_src_list($base_url, $CI->config->item('rows_per_page'), $offset, $sort_field, $sort_type);        
        $this->module_toolbar('configurations.toolbar_contactform');
        $this->module_navigation('MKT Source parameters');
	}
	
    function _mkt_src_list($base_url, $limit, &$offset=0, &$sort_field=PARAMETER_PARAM_ID, &$sort_type='0'){
		$CI = & get_instance();
        
        $config['uri_segment'] = $offset;
        $offset = $CI->uri->segment($offset)?$CI->uri->segment($offset):0;
        $sort_field = $CI->uri->segment($sort_field)?$CI->uri->segment($sort_field):PARAMETER_PARAM_ID;
        $sort_type = $CI->uri->segment($sort_type)?'1':'0';		
        $data = $CI->configuration_model->get_mkt_src_lists($limit, $offset, $sort_field, $sort_type);
        $this->add('data', $data);

        $CI->load->library('pagination');
        $config['base_url'] = site_url($base_url.'/%s/'.$sort_field.'/'.$sort_type);
        $config['total_rows'] = $CI->configuration_model->get_numrows();
        $config['per_page'] = $limit;
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
        $this->add('type', 2);
		
		$this->add('param_idTXT', $this->sort_column('#', PARAMETER_PARAM_ID, $sort_type, $base_url.'/'.$offset, $sort_field));
        $this->add('nameTXT', $this->sort_column('Name', PARAMETER_NAME, $sort_type, $base_url.'/'.$offset, $sort_field));
       
        $this->view('list_lists');
	}
}